//
//  studyPracticeView.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/4/16.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "studyPracticeView.h"
#import "practiceCell.h"
#define  classCount  self.chooseCount

@interface studyPracticeView ()<UICollectionViewDelegate,UICollectionViewDataSource,practiceCellDelegate>
@property (strong, nonatomic) IBOutlet UIButton *up_btn;
@property (strong, nonatomic) IBOutlet UIButton *next_btn;
@property (strong, nonatomic) IBOutlet UITextView *content_textView;
@end
@implementation studyPracticeView
{
    NSInteger count;//页码
    NSArray *countArr;
    NSDictionary *chooseDic;//问题数组
    NSDictionary *answerDic;//答案数组
    NSMutableArray *jiexiSaveArr;//解析数组
    NSMutableArray *chooseSaveArr;//选择数组
    NSDictionary *chooseSaveDic;
}

- (void)drawRect:(CGRect)rect {
    [self.collection_view registerNib:[UINib nibWithNibName:@"practiceCell" bundle:nil] forCellWithReuseIdentifier:@"practiceCell"];
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    [self setBottomView];
    self.content_textView.font = self.next_btn.titleLabel.font = self.up_btn.titleLabel.font = AutoLabFont2;
    jiexiSaveArr = [NSMutableArray array];
    chooseSaveArr = [NSMutableArray array];
}

#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.getArr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.collection_view.frame.size.width,self.collection_view.frame.size.height);
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//行间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    practiceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"practiceCell" forIndexPath:indexPath];
    cell.delegate = self;
    if (self.getArr.count > 0) {
        
        for (int i = 0; i<_chooseCount; i++) {
            NSString *str = @"";
            [jiexiSaveArr addObject:str];
            NSDictionary *dic = [NSDictionary dictionary];
            [chooseSaveArr addObject:dic];
        }
        
        [self.up_btn setHidden:NO];
        cell.dic = [self.getArr[indexPath.item] mj_keyValues];
        cell.currentInt = indexPath.row;
        cell.totalInt = self.chooseCount;
        answerDic = [[self.getArr[indexPath.item] mj_keyValues][@"arrayCourseAnswer"] mj_JSONObject];
        chooseDic = [[self.getArr[indexPath.item] mj_keyValues][@"arrayCourseChose"] mj_JSONObject];
        NSArray *keys = [chooseDic allKeys];
        NSArray *values = [chooseDic allValues];
        NSString *keyStr;
        NSString *valueStr;
        NSMutableArray *chooseArr = [NSMutableArray array];
        for (int i = 0; i<values.count; i++) {
            keyStr = keys[i];
            valueStr = values[i];
            NSString *question = [NSString stringWithFormat:@"%@%@",keyStr,valueStr];
            [chooseArr addObject:question];
        }
        cell.chooseArr = chooseArr;
        [cell.table reloadData];
    }
    return cell;
}

#pragma mark --- 点击选项practiceCellDelegate
-(void)chooseSelect:(NSDictionary *)dic currentPage:(NSInteger)currentPage{
    self.content_textView.text = [NSString stringWithFormat:@"正确答案：%@\r\n问题解析：%@",dic[@"courseAnswer"],dic[@"answerAnalysis"]];
    

    [jiexiSaveArr insertObject:[NSString stringWithFormat:@"%@",self.content_textView.text] atIndex:currentPage];
    [jiexiSaveArr removeObjectsInRange:NSMakeRange(_chooseCount, jiexiSaveArr.count - _chooseCount)];
    
    [chooseSaveArr insertObject:dic atIndex:currentPage];
    [chooseSaveArr removeObjectsInRange:NSMakeRange(_chooseCount, chooseSaveArr.count - _chooseCount)];
}



//底部加减按钮
-(void)setBottomView{
    [self.next_btn setHidden:YES];
    [self.up_btn setHidden:YES];
    [self.up_btn setTitle:@"下一题" forState:UIControlStateNormal];
    self.next_btn.titleLabel.font = self.up_btn.titleLabel.font = AutoLabFont2;
    count = 1;
}


//上一题按钮
- (IBAction)upBtn:(id)sender {
    if ([self.up_btn.titleLabel.text isEqualToString:@"下一题"]) {
        count++;
        [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        if (count > classCount) {
               [self.vc ShowNewMsg:@"当前已是最后一题"];
           }
    }else{
        [self.next_btn setHidden:NO];
        [sender setTitle:@"上一题" forState:UIControlStateNormal];
        if (count <= classCount && count >1) {
            count --;
            [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }
    }
}

//下一题按钮
- (IBAction)next_btn:(id)sender {
    count ++;
    if (count > classCount) {
        count = classCount;
    }
    [self.collection_view scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    if (count == classCount) {
        [self.vc ShowNewMsg:@"当前已是最后一题"];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int cuttentIndex = (int)(scrollView.contentOffset.x + self.collection_view.frame.size.width/2)/self.collection_view.frame.size.width;
    if (cuttentIndex+1 >= 2) {
        [self.next_btn setHidden:NO];
        count = cuttentIndex + 1;
        [self.up_btn setTitle:@"上一题" forState:UIControlStateNormal];
    }else{
        if (cuttentIndex == 0) {
            count = 1;
        }else{
        count = cuttentIndex;
        }
    }
    self.content_textView.text = jiexiSaveArr[cuttentIndex];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshData" object:nil userInfo:chooseSaveArr[cuttentIndex]];
}

@end
